#ifndef _PAGEFRAME_ALLOCATOR_H_
#define _PAGEFRAME_ALLOCATOR_H_

#include <stdlib.h>
#include "type.h"
#include "const.h"
#include "knlvar.h"
#include "Page.h"
#include "MemoryZone.h"
#include "../core/Resource.h"

#define ZONE_NR				2

class CPageFrameAllocator : public Callable
{
public:
	CPageFrameAllocator(int pg_count);
	~CPageFrameAllocator() {}	

	/* Return page desc */
	// request 2^order contiguous page frames
	Page * GetPagesDesc(int order);
	// request one page frame
	Page * GetPageDesc()
	{ return GetPagesDesc(0); }

	/* Return page linear address */
	// request 2^order contiguous page frames
	addr_t GetPagesAddr(int order)
	{
		Page * pg = GetPagesDesc(order);
		if(pg==NULL)
			return 0;
		else
			return (pg-mem_map)<<12;
	}
	// request one page frame
	addr_t GetPageAddr()
	{ return GetPagesAddr(0); }

	/* request one page frame filled with zeros */
	Page * GetZeroedPageDesc();
	addr_t GetZeroedPageAddr();
	/* request 2^order contiguous page frames in DMA zone */
	Page * GetDMAPagesDesc(int order);
	addr_t GetDMAPagesAddr(int order);

	void FreePagesDesc(Page * page, int order);
	void FreePagesAddr(addr_t addr, int order);
	void FreePageDesc(Page *page) { FreePagesDesc(page, 0); }
	void FreePageAddr(addr_t addr) { FreePagesAddr(addr, 0); }

private:
	/* All page frame desc */
	static Page *mem_map;
	/* Memory zone: DMA-Zone, Normal-Zone */
	static MemoryZone mem_zone[ZONE_NR];
};

#endif

